package com.ppt.android.base.utils

import android.text.TextUtils
import com.ppt.android.base.ktx.isZero
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat

/**
 *
 * @author ZouJiaCai
 * @since 2023/7/15 19:33
 */
object StringUtils {
    /**
     * 如果数字大于等于10000，它将在后面拼接"万"，并保留1小数
     */
    fun formatNumberInWan1(string: String?): String{
        if (string == null || TextUtils.isEmpty(string)) return  ""
        if (string == "--") return "--"
        val number = string.toDoubleOrNull()
        return if (number != null) {
            val result = if (number >= 10000) {
                val decimalFormat = DecimalFormat("#.0")
                decimalFormat.roundingMode = RoundingMode.FLOOR
                val formattedNumber = decimalFormat.format(number / 10000)
                val bd = BigDecimal(formattedNumber)
                "${bd.stripTrailingZeros().toPlainString()}万"
            } else {
                if (number.isZero()){
                    "0"
                }else{
                    val bd = BigDecimal(number.toString())
                    bd.stripTrailingZeros().toPlainString()
                }
            }
            (result)
        } else {
            LogUtils.e("输入的字符串不是有效的数字")
            ""
        }
    }
}